import { AwakeSystem } from "../../../../../Core/Module/EventSystem/IAwakeSystem";
import { DestroySystem } from "../../../../../Core/Module/EventSystem/IDestroySystem";
import { LateUpdateSystem } from "../../../../../Core/Module/EventSystem/ILateUpdateSystem";
import { NetServices } from "../../../../../Core/Module/Network/NetServices";
import { ExpandOf, FriendOf, ObjectSystemAttribute, Sleep } from "../../../../../Decorator/Decorator";
import { NetThreadComponent } from "../../../../Model/Share/Module/Message/NetThreadComponent";

/**
 * 使用 Expand 扩展方法
 * 
 * 必须作出以下申明，才能调用对象的扩展方法
 */
declare module "../../../../Model/Share/Module/Message/NetThreadComponent" {

    interface NetThreadComponent {

        MainThreadUpdate(): void;
        NetThreadUpdate(): void;

    }

}

@ExpandOf(NetThreadComponent)
export class NetThreadComponentSystem {

    private static MainThreadUpdate(self: NetThreadComponent): void {
        NetServices.Instance.UpdateInMainThread();
    }

    private static NetThreadUpdate(self: NetThreadComponent): void {
        // while (!self.isStop) {
        //     NetServices.Instance.UpdateInNetThread();
        //     Sleep(1);
        // }
        NetServices.Instance.UpdateInNetThread();
    }

}

@FriendOf(NetThreadComponent)
@ObjectSystemAttribute()
export class NetThreadComponentAwakeSystem extends AwakeSystem<NetThreadComponent> {

    Awake(self: NetThreadComponent): void {
        NetThreadComponent.Instance = self;
    }

}

@FriendOf(NetThreadComponent)
@ObjectSystemAttribute()
export class NetThreadComponentLateUpdateSystem extends LateUpdateSystem<NetThreadComponent> {

    LateUpdate(self: NetThreadComponent): void {
        self.MainThreadUpdate();
        self.NetThreadUpdate();
    }

}

@FriendOf(NetThreadComponent)
@ObjectSystemAttribute()
export class NetThreadComponentDestroySystem extends DestroySystem<NetThreadComponent> {

    Destroy(self: NetThreadComponent): void {
        NetThreadComponent.Instance = null;
        self.isStop = true;
    }

}